package com.icangya.interceptor;

import com.icangya.Value.FootBall;
import com.icangya.Value.StaticValue;
import com.icangya.domain.BadUserDO;
import com.icangya.domain.User;
import com.icangya.service.IAdminUserService;
import com.icangya.service.IBadUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

import static com.icangya.Value.StaticValue.GAME_USER_SESSION;

/**
 * Created by 王小红 on 2017/5/16.
 */
public class GameLoginInterceptor extends HandlerInterceptorAdapter {

    private Logger logger = Logger.getLogger(this.getClass());


    @Autowired
    private IAdminUserService iAdminUserService;

    @Autowired
    private IBadUserService iBadUserService;
    //@Autowired
   // IUserDAO iUserDAO;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        /** 拦截到用户的请求了 */
        String requestUrl = request.getRequestURL().toString();
        /** */
        logger.debug("requestUrl:" + requestUrl);
        User user = (User) request.getSession().getAttribute(FootBall.FOOT_USER_SESSION);

//        if (StaticValue.DEBUG && user == null) {
//            response.sendRedirect("http://www.google.com");
//            return false;
//        }

        if(iAdminUserService.getSwitch().getStatus()==0){
            try {
                response.sendRedirect("http://www.gentaiyuan.cn/sys/wh.html");
                return false;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (StaticValue.WX.BASE_URL.equals(requestUrl)||(StaticValue.WX.BASE_URL+"/").equals(requestUrl)){
            //如果地址等于www.mnuocom.com,则跳转到百度
            response.sendRedirect("https://www.baidu.com/");
            return false;

        }
        /** 判断session是否存在用户,如果存在说明用户已经登录了,应该放行 */
        if (user != null) {

            //拉黑用户检测，并且拒绝其访问
            List<BadUserDO> badUserDOList = iBadUserService.getBadUserByOpenid(user.getOpenId());
            if (badUserDOList.size() > 0){
                response.sendRedirect("http://fanyi.baidu.com/translate#en/zh");
                return false;
            }

            /** 当前请求：每个请求是否都是一个线程   */
//            if ("oyGuGwv-FXKvW9_rbzfDXqdBNZBo".equals(user.getOpenId())||"oyGuGwu-Avfv6SYRrzCV5wzDcDa8".equals(user.getOpenId())||"oyGuGwmC8IJb1Y2GfOz7Zd0xA9Cc".equals(user.getOpenId())||"oyGuGwhuK0PKjNvFgexwReQLbAE8".equals(user.getOpenId())||"oyGuGwq1sVErMBR9JRIVaelQXFfk".equals(user.getOpenId())){
//                return true;
//            }

            return true;
        } else {
            // 重定向
            response.sendRedirect("http://www.xsrkej.com/");
            logger.debug("requestUrl:" + requestUrl + "->被拦截！");
            return false;
        }

    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }


}
